package youcaihuaoa.weetop.com.ui.splash

import android.os.Bundle
import android.text.TextUtils
import com.blankj.utilcode.util.LogUtils
import youcaihuaoa.weetop.com.R
import youcaihuaoa.weetop.com.databinding.AppActivitySplashBinding
import youcaihuaoa.weetop.com.vm.splash.SplashViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.configs.PrefsManager
import com.robot.base.util.arouter.ARouterUtil
import com.tencent.bugly.crashreport.CrashReport
import io.rong.imkit.RongIM
import io.rong.imkit.config.RongConfigCenter
import io.rong.imkit.conversationlist.provider.BaseConversationProvider
import io.rong.imkit.conversationlist.provider.PrivateConversationProvider
import io.rong.imlib.RongIMClient
import me.goldze.mvvmhabit.BR
import youcaihuaoa.weetop.com.im.CustomConversationProvider
import youcaihuaoa.weetop.com.im.GLConversationFragment

/**
 * 启动页
 */
class SplashActivity : GLBaseActivity<AppActivitySplashBinding, SplashViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_splash

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        val token = PrefsManager.getUserLoginInfo().token_value
        if (TextUtils.isEmpty(token)) {
            ARouterUtil.goToActivity(AppConstants.Router.Main.A_LOGIN)
            finish()
        } else {
            loginByRongCloud()
        }
        CrashReport.initCrashReport(applicationContext, "b12c438e7d", false)
    }

    private fun loginByRongCloud() {

        val token = PrefsManager.getUserLoginInfo().ry_token
        RongIM.connect(token, object : RongIMClient.ConnectCallback() {
            override fun onSuccess(userId: String) {
                // 登录成功，跳转到默认会话列表页。
                ARouterUtil.goToActivity(AppConstants.Router.Main.A_MAIN)
                finish()
                LogUtils.d("LoginViewModel----融云登录成功")
//                RouteUtils.routeToConversationListActivity(this@LoginActivity, "")
            }

            override fun onError(connectionErrorCode: RongIMClient.ConnectionErrorCode) {
                ARouterUtil.goToActivity(AppConstants.Router.Main.A_MAIN)
                finish()
                LogUtils.d("LoginViewModel----融云登录失败--失败原因" + connectionErrorCode)
            }

            override fun onDatabaseOpened(databaseOpenStatus: RongIMClient.DatabaseOpenStatus) {}
        })
    }

}